package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.manage.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.FileTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.IamFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.manage.MaterialsOutboundMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.manage.MaterialsOutbound;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.IamOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.manage.MaterialsInventoryService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.manage.MaterialsOutboundService;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangsy on 2016/12/8.
 */
public class MaterialsOutboundServiceImpl implements MaterialsOutboundService {
    @Autowired
    private MaterialsOutboundMapper materialsOutboundMapper;
    @Autowired
    private IamFileMapper iamFileMapper;
    @Autowired
    private IamOrganizationService iamOrganizationService;
    @Autowired
    private MaterialsInventoryService materialsInventoryService;

    @Override
    public int deleteByPrimaryKey(String materialsOutboundId) {
        return materialsOutboundMapper.deleteByPrimaryKey(materialsOutboundId);
    }

    @Override
    public int insert(MaterialsOutbound record) {
        return materialsOutboundMapper.insert(record);
    }

    @Override
    public int insertSelective(MaterialsOutbound record) {
        return materialsOutboundMapper.insertSelective(record);
    }

    @Override
    public MaterialsOutbound selectByPrimaryKey(String materialsOutboundId) {
        return materialsOutboundMapper.selectByPrimaryKey(materialsOutboundId);
    }

    @Override
    public int updateByPrimaryKeySelective(MaterialsOutbound record) {
        return materialsOutboundMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MaterialsOutbound record) {
        return materialsOutboundMapper.updateByPrimaryKey(record);
    }

    @Override
    public JSONObject list(MaterialsOutbound materialsOutbound) {
        JSONObject jsonObject = new JSONObject();
        materialsOutbound.setLimit( materialsOutbound.getLimit() == null ? 10 : materialsOutbound.getLimit() );
        materialsOutbound.setOffset( materialsOutbound.getOffset() == null ? 0 : materialsOutbound.getOffset() );
        jsonObject.put("total",materialsOutboundMapper.queryCount(materialsOutbound));
        jsonObject.put("rows", JSON.toJSON(materialsOutboundMapper.queryList(materialsOutbound)));
        return jsonObject;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(MaterialsOutbound record, String uploadFiles, String deletedFiles) {
        //插入数据成功之后，返回的条数
        int resultCount = 0;
        //  根据申请人  查询申请人所属组织
        Organization organization =  null;//iamOrganizationService.organization(record.getApplyUserId());
        Employee employee = iamOrganizationService.employee(record.getApplyUserId());
        String applyUnit = "";
        if(StringUtils.isNotEmpty(employee.getDepId())){
            applyUnit = employee.getDepId();
        }else{
            applyUnit = employee.getOrgId();
        }
        record.setApplyUnit(applyUnit);
        //  根据领料人 查询领料人所属组织
        employee = iamOrganizationService.employee(record.getPickingManId());
        if(StringUtils.isNotEmpty(employee.getDepId())){
            applyUnit = employee.getDepId();
        }else{
            applyUnit = employee.getOrgId();
        }
        record.setPickingUnitId(applyUnit);
        //  根据领料人 查询所属作业区
        organization = iamOrganizationService.getWorkZone(record.getPickingManId());
        record.setInventoryId(organization.getOrgId());

        //保存业务数据
        resultCount =  materialsOutboundMapper.insertSelective(record);
        //更新的附件不为空的情况
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                String pId = record.getMaterialsOutboundId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                resultCount += iamFileMapper.insert(listFiles);
            }
        }
        //插入数据成功之后还要写入库存
        resultCount +=  materialsInventoryService.updateByMaterialsOutboundInfo(record);
        return resultCount;
    }
}
